package cn.charmaine.org.controller;

import cn.charmaine.basic.util.AjaxResult;
import cn.charmaine.basic.util.PageList;
import cn.charmaine.org.domain.DetailPage;
import cn.charmaine.org.domain.Shop;
import cn.charmaine.org.domain.SystemDictionaryDetail;
import cn.charmaine.org.service.ISystemDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class SystemDictionaryDetailController {
    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    //分页查询
    @PostMapping
    public PageList<SystemDictionaryDetail> selectData(@RequestBody DetailPage detail) {
        return systemDictionaryDetailService.queryByTypeId(detail);
    }

    //根据id删除
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        try{
            systemDictionaryDetailService.deleteById(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.thisSuccess().setSuccess(false).setMessage("操作失败");
        }
    }

    //增加或者修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail){
        try {
            if( systemDictionaryDetail.getId() != null) {
                systemDictionaryDetailService.update(systemDictionaryDetail);
            }else {
                System.out.println(systemDictionaryDetail);
                systemDictionaryDetailService.add(systemDictionaryDetail);
            }
            return AjaxResult.thisSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.thisSuccess().setSuccess(false).setMessage("操作失败");
        }
    }
}
